import React, { Component } from 'react';
import {
  StyleSheet,
  View,
  Text,
  Image,
  ActivityIndicator,
  AsyncStorage,
  TouchableOpacity,
  Linking,
  ToastAndroid,
  TextInput,
  Button,
} from 'react-native';


const yanzheng=''

export default class Login extends Component{
    static navigationOptions = {
        title: '快速登录',
        headerTitleStyle: {
            alignSelf:'center'
          },
      };
      state = {
        codeSent:false,
        phoneNumber:0,
        phonecode:0
      };
      getcode= async function(){
           if(!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(this.state.phoneNumber))){ 
                ToastAndroid.show('请输入正确的手机号', ToastAndroid.SHORT);
                return false; 
            } 
          try {
            let response = await fetch(global.mainurl+"bossHelper/getcode?account="+this.state.phoneNumber);
          console.log(response)
            var data=await response.json()
            console.log(data)
                    if(data.code==1){
                        if(data.user){
                            global.sellerid=data.data.user.office.id;
                            global.token=data.data.user.token
                            global.userdata=data.data.user
                            yanzheng=data.data.sms.param
                        }
                        else
                         ToastAndroid.show('该账户未开通', ToastAndroid.SHORT);
                    }
                    else if(data.code==0){
								ToastAndroid.show('未知错误', ToastAndroid.SHORT);
							}
         
            } catch(error) {
                console.log(error)
            ToastAndroid.show('网络出错', ToastAndroid.SHORT);
            }
      }
      _sendVerifyCode= ()=>{
          this.setState(
              {
                  codeSent:true
              }
          )
      }
      _submit= ()=>{
          if(yanzheng==this.state.phonecode){
              ToastAndroid.show('登陆成功', ToastAndroid.SHORT);
              this.props.navigation.navigate("MyTab")
          }
          else{
               ToastAndroid.show('验证码不正确', ToastAndroid.SHORT);
          }
      }
      render(){
          return(
            <View style={styles.container}>
            <View style={styles.signupBox}>
                
                <TextInput
                 placeholder="输入手机号"
                 autoCapitalize={'none'}
                 autoCorrect={false}
                 autoFocus={false}
                 underlineColorAndroid='transparent'
                 editable={true}
                 multiline={false}
                 keyboardType={'number-pad'}
                 style={styles.inputField}
                 onChangeText={(text)=>{
                     this.setState({
                         phoneNumber:text
                     })
                 }}
                 />
                 <TextInput
                 placeholder="输入验证码"
                 autoCapitalize={'none'}
                 autoCorrect={false}
                 autoFocus={false}
                 underlineColorAndroid='transparent'
                 editable={true}
                 multiline={false}
                 keyboardType={'number-pad'}
                 style={styles.inputField}
                 onChangeText={(text)=>{
                     this.setState({
                         phonecode:text
                     })
                 }}
                 />
             

                 
                     
                     <Button
                       style={styles.btn}
                       onPress={()=>{this._submit()}} title="登录"/>
                       <Text style={{height:10}}></Text>
                     <Button
                       style={styles.btn}
                       onPress={()=>{this.getcode()}} title="获取验证码"/>
                 
            </View>
            <View style={styles.otherbox}>
            <View style={styles.other1}>
            <Image source={require('../img/poster.jpg')} style={styles.img}></Image>
            <Button onPress={
                ()=>{
                    this.props.navigation.navigate("Login1")
                }
            }
             title="账号密码登录" style={styles.other}/>
            </View>
            </View>
            </View>
          )
      }
}


var styles=StyleSheet.create({
    container:{
        flex:1,
        padding:10,
        backgroundColor:'#f9f9f9'
    },

    signupBox:{
        marginTop:10
    },

    title:{
        marginBottom:20,
        color:'#333',
        fontSize:20,
        textAlign:'center'
    },
    inputField:{
        height:40,
        padding:5,
        color:"#666",
        fontSize:16,
        borderRadius:4,
        backgroundColor:'white',
        marginBottom:10
    },

    btn:{
        padding:40,
        backgroundColor:'transparent',
        borderColor:'#ee735c',
        borderWidth:1,
        borderRadius:4,
        color:'#ee735c'
    },
    other:{
        marginTop:50,
        textAlign:'center'
    },
    other1:{
        flexDirection:'column',
        alignItems:'center',
    },
    otherbox:{
        marginTop:50,
        height:200,
        flexDirection:'column',
        alignItems:'center',
        justifyContent:'space-between'
    },
    img:{
        width:50,
        height:50,
        borderRadius:50,
        marginBottom:20
    }
})